using System.Linq;
using System.Web.Mvc;
using System.Web;
using WRSManagementSystem.DomainModel.Entities;
using WRSManagementSystem.DomainModel.Repositories;

namespace WRSManagementSystem.Controllers
{

    public class PeopleController : Controller
    {
        public string magicPath = "~/foto";
        
        private readonly IPeopleRepository _peoplerepo;

        public PeopleController(IPeopleRepository peoplerepo)
        {
            _peoplerepo = peoplerepo;
        }

        //
        // GET: /People/

        public ActionResult Index()
        {
            return View(_peoplerepo.Find().OrderBy(p => p.Surname));
        }

        //
        // GET: /People/Details/5

        public ActionResult Details(int id)
        {
            return View(_peoplerepo.FindOne(p => p.Id == id));
        }

        //
        // GET: /People/Create

        public ActionResult Create()
        {
            return View(new Person());
        }

        //
        // POST: /People/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Person model, HttpPostedFileBase FotoFoto)
        {
           // System.IO.File.Copy(fc["fotopath"], string.Format("\{0}_{1}.png")
            string photoPath = HttpContext.ApplicationInstance.Server.MapPath(magicPath);
            if (FotoFoto == null)
            {
                return View(model);
            }
            var di = new System.IO.DirectoryInfo(photoPath);
            var foto_name = model.Name + "_" + model.Surname + FotoFoto.FileName;

            FotoFoto.SaveAs(di.FullName + "/" + foto_name);
            model.FotoFoto = foto_name;

            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    _peoplerepo.Add(model);
                    _peoplerepo.Save();

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
            return View(model);
        }

        //
        // GET: /People/Edit/5

        public ActionResult Edit(int id)
        {
            return View(_peoplerepo.FindOne(p => p.Id == id));
        }

        //
        // POST: /People/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Person model, HttpPostedFileBase FotoFoto)
        {
            if (FotoFoto != null)
            {
                string photoPath = HttpContext.ApplicationInstance.Server.MapPath(magicPath);
                var di = new System.IO.DirectoryInfo(photoPath);
                System.IO.File.Delete(di.FullName + "/" + model.FotoFoto);
                var foto_name = model.Name + "_" + model.Surname + FotoFoto.FileName;
                FotoFoto.SaveAs(di.FullName + "/" + foto_name);
                model.FotoFoto = foto_name;
            }

            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add update logic here
                    //_peoplerepo.Save(model);                    
                    _peoplerepo.Update(model);
                    _peoplerepo.Save();                    

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
            return View(model);
        }
    }
}